package com.example.rwq_test.threadLocal.sdf;

import com.example.rwq_test.constant.TimeConst;
import com.example.rwq_test.threadLocal.MyThreadLocalUtil;

import java.text.SimpleDateFormat;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * TODO note
 *
 * @author renweiqiang
 * @version 1.0.0
 * @createTime 2024/4/12
 */
public class MySDFThreadFactory implements ThreadFactory {
    private static final AtomicInteger THREAD_NUM = new AtomicInteger(0);

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(() -> {
            // 设置线程名字
            int threadNum = THREAD_NUM.getAndIncrement();
            Thread.currentThread().setName("Thread-" + threadNum);
            // 设置每个线程的SDF类型数据
            ThreadLocal<SimpleDateFormat> sdfThreadLocal = MyThreadLocalUtil.getSdfThreadLocal();
            sdfThreadLocal.set(new SimpleDateFormat(TimeConst.TIMESTAMP_PATTERN));
            // 执行线程任务
            r.run();
            // 打印ThreadLocal的SDF值
            System.out.println("sdfThreadLocal value: " + sdfThreadLocal.get());
        });
    }
}
